//
// Created by wirano on 23-2-18.
//

#ifndef ST77903_INIT_PARSE_H
#define ST77903_INIT_PARSE_H

// support lcd modules: DXQ1D602, YDP160B001_V6, H0165Y003_V1, H0165Y005T005B_V0
#define LCD_MODULE          YDP160B001_V6
// set color depth 16 for RGB565, 18 for RGB666 24 for RGB888
#define LCD_BPP             (16)

//#define BIST_MODE

#if (LCD_MODULE == DXQ1D602) || (LCD_MODULE == YDP160B001_V6) || (LCD_MODULE == H0165Y003_V1)
#define LCD_X_SIZE          (400U)                      /* available x pixel size */
#define LCD_Y_SIZE          (400U)                        /* available y pixle size */
#define LCD_PBYTE           ((LCD_BPP + 7) / 8)         /* bytes in pixel unit */
#define LCD_HBYTE           (LCD_X_SIZE * LCD_PBYTE)    /* bytes in horizontal line */

#define LCD_VSW             (1U)
#define LCD_HFP             (8U)
#define LCD_HBP             (8U)
#endif

#define CMD_DATA_LEN_MAX    (14U)

#if (LCD_BPP == 16)
#define CMD_PIXEL_FORMAT    (0x05)
#elif (LCD_BPP == 18)
#define CMD_PIXEL_FORMAT    (0x06)
#elif (LCD_BPP == 24)
#define CMD_PIXEL_FORMAT    (0x07)
#endif

typedef struct {
    uint8_t cmd;
    uint8_t len;
    uint8_t data[CMD_DATA_LEN_MAX];
} st77903_init_para_t;

/* ST77903 LCD Initialize table */
/* command - parameter length - parameter data */
/* if command is 0xff, means a delay time unit millisecond */
static const st77903_init_para_t st77903_init_parameter[] = {
#if (LCD_MODULE == DXQ1D602)
        {0xf0, 1, {0xc3}},
        {0xf0, 1, {0x96}},
        {0xf0, 1, {0xa5}},
        {0xe9, 1, {0x20}},
        {0xe7, 4, {0x80, 0x77, 0x1f, 0xcc}},
        {0xc1, 4, {0x77, 0x07, 0xcf, 0x16}},
        {0xc2, 4, {0x77, 0x07, 0xcf, 0x16}},
        {0xc3, 4, {0x22, 0x02, 0x22, 0x04}},
        {0xc4, 4, {0x22, 0x02, 0x22, 0x04}},
        {0xc5, 1, {0xed}},
        {0xe0, 14,
         {0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b,
          0x17, 0x2a, 0x2f}},
        {0xe1, 14,
         {0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b,
          0x17, 0x2a, 0x2f}},
        {0xe5, 14,
         {0xbe, 0xf5, 0xb1, 0x22, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22,
          0x22, 0x22, 0x22}},
        {0xe6, 14,
         {0xbe, 0xf5, 0xb1, 0x22, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22,
          0x22, 0x22, 0x22}},
        {0xec, 2, {0x40, 0x03}},
        {0xb2, 1, {0x00}},
        {0xb3, 1, {0x01}},
        {0xb4, 1, {0x00}},
        {0xb5, 4, {0x00, 0x08, 0x00, 0x08}},
        {0xa5, 9, {0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x2a, 0x8a, 0x02}},
        {0xa6, 9, {0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x2a, 0x8a, 0x02}},
        {0xba, 7, {0x0a, 0x5a, 0x23, 0x10, 0x25, 0x02, 0x00}},
        {0xbb, 8, {0x00, 0x30, 0x00, 0x2c, 0x82, 0x87, 0x18, 0x00}},
        {0xbc, 8, {0x00, 0x30, 0x00, 0x2c, 0x82, 0x87, 0x18, 0x00}},
        {0xbd, 11,
         {0xa1, 0xb2, 0x2b, 0x1a, 0x56, 0x43, 0x34, 0x65, 0xff, 0xff, 0x0f}},
        {0x35, 1,
         {0x00}},  // enable Tearing Effect line on, mode 0, only V-Blanking information
        {0x36, 1, {0x0c}},
        {0x3a, 1, {CMD_PIXEL_FORMAT}},
        {0x21, 0, {0x00}},
        {0x11, 0, {0x00}},
        {0xff, 1, {120}},
        {0x29, 0, {0x00}},
        {0xff, 1, {120}},
#elif (LCD_MODULE == H0165Y003_V1)
        {0xf0, 1, {0xc3}},
                {0xf0, 1, {0x96}},
                {0xf0, 1, {0xa5}},
                {0xe9, 1, {0x20}},
                {0xe7, 4, {0x80, 0x77, 0x1f, 0xcc}},
                {0xc1, 4, {0x77, 0x07, 0xc2, 0x07}},
                {0xc2, 4, {0x77, 0x07, 0xc2, 0x07}},
                {0xc3, 4, {0x22, 0x02, 0x22, 0x04}},
                {0xc4, 4, {0x22, 0x02, 0x22, 0x04}},
                {0xc5, 1, {0x71}},
                {0xe0, 14,
                 {0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b,
                  0x17, 0x2a, 0x2f}},
                {0xe1, 14,
                 {0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b,
                  0x17, 0x2a, 0x2f}},
                {0xe5, 14,
                 {0xb2, 0xf5, 0xbd, 0x24, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22,
                  0x22, 0x22, 0x22}},
                {0xe6, 14,
                {0xb2, 0xf5, 0xbd, 0x24, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22,
                 0x22, 0x22, 0x22}},
                {0xec, 2, {0x40, 0x03}},
                {0x36, 1, {0x0c}},
                {0x3a, 1, {CMD_PIXEL_FORMAT}},
                {0xb2, 1, {0x00}},
                {0xb3, 1, {0x01}},
                {0xb4, 1, {0x00}},
                {0xb5, 4, {0x00, 0x08, 0x00, 0x08}},
                {0xa5, 9, {0x00, 0x00, 0x00, 0x00, 0x20, 0x15, 0x2a, 0x8a, 0x02}},
                {0xa6, 9, {0x00, 0x00, 0x00, 0x00, 0x20, 0x15, 0x2a, 0x8a, 0x02}}, //only to here
                {0xba, 7, {0x0a, 0x5a, 0x23, 0x10, 0x25, 0x02, 0x00}},
                {0xbb, 8, {0x00, 0x2d, 0x00, 0x25, 0x88, 0x87, 0x18, 0x00}},
                {0xbc, 8, {0x00, 0x30, 0x00, 0x29, 0x88, 0x87, 0x18, 0x00}},
                {0xbd, 11,
                {0xa1, 0xb2, 0x2b, 0x1a, 0x56, 0x43, 0x34, 0x65, 0xff, 0xff, 0x0f}},
                {0x35, 1,
                {0x00}},  // enable Tearing Effect line on, mode 0, only V-Blanking information
                {0x21, 0, {0x00}},
                {0x11, 0, {0x00}},
                {0xff, 1, {120}},
                {0x29, 0, {0x00}},
                {0xff, 1, {120}},

#elif (LCD_MODULE == H0165Y005T005B_V0)
                {0xf0, 1, {0xc3}},
                {0xf0, 1, {0x96}},
                {0xf0, 1, {0xa5}},
                {0xe9, 1, {0x20}},
                {0xe7, 4, {0x80, 0x77, 0x1f, 0xcc}},
                {0xc1, 4, {0x77, 0x07, 0xcf, 0x16}},
                {0xc2, 4, {0x77, 0x07, 0xcf, 0x16}},
                {0xc3, 4, {0x22, 0x02, 0x22, 0x04}},
                {0xc4, 4, {0x22, 0x02, 0x22, 0x04}},
                {0xc5, 1, {0xed}},
                {0xe0, 14,
                 {0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b,
                  0x17, 0x2a, 0x2f}},
                {0xe1, 14,
                 {0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b,
                  0x17, 0x2a, 0x2f}},
                {0xe5, 14,
                 {0xbe, 0xf5, 0xb1, 0x22, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22,
                  0x22, 0x22, 0x22}},
                {0xe6, 14,
                {0xbe, 0xf5, 0xb1, 0x22, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22,
                 0x22, 0x22, 0x22}},
                {0xec, 2, {0x40, 0x03}},
                {0x36, 1, {0x0c}},
                {0x3a, 1, {CMD_PIXEL_FORMAT}},
                {0xb2, 1, {0x00}},
                {0xb3, 1, {0x01}},
                {0xb4, 1, {0x00}},
                {0xb5, 4, {0x00, 0x08, 0x00, 0x08}},
                {0xa5, 9, {0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x2a, 0x8a, 0x02}},
                {0xa6, 9, {0x00, 0x00, 0x00, 0x00, 0x20, 0x15, 0x2a, 0x8a, 0x02}}, // only to here
                {0xba, 7, {0x0a, 0x5a, 0x23, 0x10, 0x25, 0x02, 0x00}},
                {0xbb, 8, {0x00, 0x2d, 0x00, 0x25, 0x88, 0x87, 0x18, 0x00}},
                {0xbc, 8, {0x00, 0x30, 0x00, 0x29, 0x88, 0x87, 0x18, 0x00}},
                {0xbd, 11,
                {0xa1, 0xb2, 0x2b, 0x1a, 0x56, 0x43, 0x34, 0x65, 0xff, 0xff, 0x0f}},
                {0x35, 1,
                {0x00}},  // enable Tearing Effect line on, mode 0, only V-Blanking information
                {0x21, 0, {0x00}},
                {0x11, 0, {0x00}},
                {0xff, 1, {120}},
                {0x29, 0, {0x00}},
                {0xff, 1, {120}},

#elif (LCD_MODULE == YDP1608001_V6)
                {0xf0, 1, {0xc3}},
                {0xf0, 1, {0x96}},
                {0xf0, 1, {0xa5}},
                {0xe9, 1, {0x20}},
                {0xe7, 4, {0x80, 0x77, 0x1f, 0xcc}},
                {0xc1, 4, {0x77, 0x07, 0xc2, 0x07}},
                {0xc2, 4, {0x77, 0x07, 0xc2, 0x07}},
                {0xc3, 4, {0x22, 0x02, 0x22, 0x04}},
                {0xc4, 4, {0x22, 0x02, 0x22, 0x04}},
                {0xc5, 1, {0x71}},
                {0xe0, 14,
                 {0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b,
                  0x17, 0x2a, 0x2f}},
                {0xe1, 14,
                 {0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b,
                  0x17, 0x2a, 0x2f}},
                {0xe5, 14,
                 {0xb2, 0xf5, 0xbd, 0x24, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22,
                  0x22, 0x22, 0x22}},
                {0xe6, 14,
                {0xb2, 0xf5, 0xbd, 0x24, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22,
                 0x22, 0x22, 0x22}},
                {0xec, 2, {0x40, 0x03}},
                {0x36, 1, {0x0c}},
                {0x3a, 1, {CMD_PIXEL_FORMAT}},
                {0xb2, 1, {0x00}},
                {0xb3, 1, {0x01}},
                {0xb4, 1, {0x00}},
                {0xb5, 4, {0x00, 0x08, 0x00, 0x08}},
                {0xa5, 9, {0x00, 0x00, 0x00, 0x00, 0x20, 0x15, 0x2a, 0x8a, 0x02}},
                {0xa6, 9, {0x00, 0x00, 0x00, 0x00, 0x20, 0x15, 0x2a, 0x8a, 0x02}},
                {0xba, 7, {0x0a, 0x5a, 0x23, 0x10, 0x25, 0x02, 0x00}},
                {0xbb, 8, {0x00, 0x2d, 0x00, 0x25, 0x88, 0x87, 0x18, 0x00}},
                {0xbc, 8, {0x00, 0x30, 0x00, 0x29, 0x88, 0x87, 0x18, 0x00}},
                {0xbd, 11,
                {0xa1, 0xb2, 0x2b, 0x1a, 0x56, 0x43, 0x34, 0x65, 0xff, 0xff, 0x0f}},
                {0x35, 1,
                {0x00}},  // enable Tearing Effect line on, mode 0, only V-Blanking information
                {0x21, 0, {0x00}},
                {0x11, 0, {0x00}},
                {0xff, 1, {120}},
                {0x29, 0, {0x00}},
                {0xff, 1, {120}},
#endif

#ifdef BIST_MODE
        {0xb0,  1, {0xa5}},
        {0xcc,  9, {0x40, 0x00, 0x3f, 0x00, 0x14, 0x14, 0x20, 0x20, 0x03}},
#endif
};

#endif //ST77903_INIT_PARSE_H
